package com.koron.ticket.workchange.ticketchangedelay.service.impl;


import com.koron.bean.base.Response;
import com.koron.common.core.util.CodeTools;
import com.koron.ticket.workchange.ticketchangedelay.bean.TicketChangeDelay;
import com.koron.ticket.workchange.ticketchangedelay.bean.TicketChangeDelayQuery;
import com.koron.ticket.workchange.ticketchangedelay.mapper.TicketChangeDelayMapper;
import com.koron.ticket.workchange.ticketchangedelay.service.TicketChangeDelayService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (TicketChangeDelay)表服务实现类
 *
 * @author 朱超
 * @since 2021-10-27 09:28:01
 */
@Service
public class TicketChangeDelayServiceImpl implements TicketChangeDelayService {

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("insert")
    public Response insert(SessionFactory factory, TicketChangeDelay ticketChangeDelay) {
        TicketChangeDelayMapper mapper = factory.getMapper(TicketChangeDelayMapper.class);
        Date delayTime = ticketChangeDelay.getDelayTime();
        String workTicketId = ticketChangeDelay.getWorkTicketId();

        // 1. 根据工作票票号查找最新的延期时间
        Date lastDelayTime = mapper.getLastDelayTime(workTicketId);
        if (lastDelayTime != null && delayTime.before(lastDelayTime)) {
            return Response.fail("延期时间必须大于该工作票最新的延期时间");
        }

        String id = CodeTools.getCode32();
        ticketChangeDelay.setId(id);
        mapper.insert(ticketChangeDelay);
        return Response.success(id);
    }

    /**
     * 根据id删除单条数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public Response deleteById(SessionFactory factory, String id) {
        TicketChangeDelayMapper mapper = factory.getMapper(TicketChangeDelayMapper.class);
        mapper.deleteById(id);
        return Response.success("删除成功");
    }

    /**
     * 根据workTicketId删除相关数据
     *
     */
    @Override
    public void deleteByWorkTicketId(SessionFactory factory, String workTicketId) {
        TicketChangeDelayMapper mapper = factory.getMapper(TicketChangeDelayMapper.class);
        mapper.deleteByWorkTicketId(workTicketId);
    }

    /**
     * 更新数据
     */
    @Override
    @TaskAnnotation("update")
    public Response update(SessionFactory factory, TicketChangeDelay ticketChangeDelay) {
        TicketChangeDelayMapper mapper = factory.getMapper(TicketChangeDelayMapper.class);
        mapper.update(ticketChangeDelay);
        return Response.success("更新成功");
    }

    /**
     * 根据id查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public Response getById(SessionFactory factory, String id) {
        TicketChangeDelayMapper mapper = factory.getMapper(TicketChangeDelayMapper.class);
        TicketChangeDelayQuery query = new TicketChangeDelayQuery();
        query.setId(id);
        TicketChangeDelay ticketChangeDelay = mapper.getSingle(query);
        return Response.success(ticketChangeDelay);
    }

    @Override
    public TicketChangeDelay getSingle(SessionFactory factory, TicketChangeDelayQuery ticketChangeDelayQuery) {
        TicketChangeDelayMapper mapper = factory.getMapper(TicketChangeDelayMapper.class);
        return mapper.getSingle(ticketChangeDelayQuery);
    }

    @Override
    public List<TicketChangeDelay> getList(SessionFactory factory, TicketChangeDelayQuery ticketChangeDelayQuery) {
        TicketChangeDelayMapper mapper = factory.getMapper(TicketChangeDelayMapper.class);
        return mapper.getList(ticketChangeDelayQuery);
    }

}
